/*
 * This project is mainly focused on developing more and more utilisties
 *  like mail utilities, dao utilities, ldap utilities, file utilities, 
 *  file importing utilities etc. for easy coding in java.
 *  
 *  Copyright (C) 2012  R.Satheesh Kumar alias GURU
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jsmart.util.core;

import org.jsmart.util.RowMapper;
import org.jsmart.util.io.file.QueryCSVCaller;

/**
 * This class acts as the base class for all Callback Query classes
 * This class helps in providing the two type constructors for diving the
 * role of this class into two. one is with callback and filter, then other one is with 
 * callback, filter and singleData boolean value. The purpose of this class is to
 * minimize code of creating separate QueryCallback for other utilities
 * 
 * @author R.Satheesh Kumar alias GURU <pulsarcoder@gmail.com>
 * @see UtilsException
 * @see AbstractUtils.AbstractCallback
 * @see AbstractUtils.AbstarctItemCallback
 * @see AbstractUtils.AbstractItemFilter
 * @see QueryReaderCaller
 * @see QueryListCaller
 * @see QueryCSVCaller
 */
public abstract class AbstractQueryCaller<S, T, U, V> implements AbstractCallback<T, S> {
    /**
     * This is the callback variable used for performing the business logic
     */
    protected AbstractItemCallback<U, V> callback;
    /**
     * This is the filter variable used for filtering the item of the list
     */
    protected AbstractItemFilter<V> filter;
    /**
     * This variable is the key to differentiate the functionality of 
     * this class doInCallback method, such that if this variable is true
     * then if filter matches the constraints it will return only one data,
     * else the doInCallback method returns all the items that matches the 
     * constraints in the list
     * By default the value of this variable is false
     */
    protected Boolean singleData = false;
    /**
     * This variable is used as the mapper for the data object of the list
     * item, the rowMapper will be injected into this class by constructor
     */
    protected RowMapper<U, V> rowMapper;
    
    public AbstractQueryCaller(){}
    
    /**
     * This constructor is used to set the callback and filter variables
     * singleData will be set false by default.
     * @param callback
     * @param filter 
     */
    public AbstractQueryCaller(AbstractItemCallback<U, V> callback,
            AbstractItemFilter<V> filter, Boolean singleData) {
        this.callback = callback;
        this.filter = filter;
        this.singleData = singleData;
    }
    
    /**
     * This constructor is used to set the callback, filter and singleData variables
     * @param callback
     * @param filter
     * @param singleData 
     */
    public AbstractQueryCaller(AbstractItemCallback<U, V> callback,
            AbstractItemFilter<V> filter) {
        this(callback, filter, false);
    }

    /**
     * This constructor is used for setting the rowMapper object
     * This constructor can provide the usage of RowMapper for the
     * subclasses that use AbstractUtils class as parent class
     * @param rowMapper
     */
    public AbstractQueryCaller(RowMapper<U, V> rowMapper) {
        this.rowMapper = rowMapper;
    }
    
    /**
     * This method helps in performing the business logic with the data.
     * All the subclasses of this class must implement this method to 
     * define the key function of logical code injected with the data.
     * @param s
     * @return 
     * @throws UtilsException
     */
    public abstract T doInCallback(S s) throws UtilsException;
}
